import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/Login.vue'
import Home from '@/views/Home.vue'
import UserManage from '@/views/main/UserManage.vue'
import BannerManage from '@/views/main/BannerManage.vue'
import DetailsManage from '@/views/main/DetailsManage.vue'
import DynamicManage from '@/views/main/DynamicManage.vue'
import JoinManage from '@/views/main/JoinManage.vue'
import ContactManage from '@/views/main/ContactManage.vue'


import BannerAdd from '@/components/banner/BannerAdd.vue'
import BannerEdit from '@/components/banner/BannerEdit.vue'

import ArtAdd from '@/components/art/ArtAdd.vue'
import ArtEdit from '@/components/art/ArtEdit.vue'

import DynamicAdd from '@/components/dynamic/DynamicAdd.vue'
import DynamicEdit from '@/components/dynamic/DynamicEdit.vue'

import ContactAdd from '@/components/contact/ContactAdd.vue'
import ContactEdit from '@/components/contact/ContactEdit.vue'

Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    redirect:'/login'
  },
  {
    path:'/login',
    component:Login
  },
  {
    path:'/home',
    component:Home,
    redirect:'/userManage',
    children:[
      //用户管理
      {
        path:'/userManage',
        component:UserManage
      },
      //轮播图管理
      {
        path:'/bannerManage',
        component:BannerManage
      },
      //添加轮播图
      {
        path:'/banner/add',
        component:BannerAdd
      },
      //编辑轮播图
      {
        path:'/banner/edit/:id',
        component:BannerEdit
      },

      //艺术涂料管理
      {
        path:'/detailsManage',
        component:DetailsManage
      },
      //添加艺术涂料
      {
        path:'/art/add',
        component:ArtAdd
      },
      //修改艺术涂料
      {
        path:'/art/edit/:id',
        component:ArtEdit
      },

      //动态管理
      {
        path:'/dynamicManage',
        component:DynamicManage
      },
      //添加艺术涂料
      {
        path:'/dynamic/add',
        component:DynamicAdd
      },
      //修改艺术涂料
      {
        path:'/dynamic/edit/:id',
        component:DynamicEdit
      },


      //培训加盟
      {
        path:'/joinManage',
        component:JoinManage
      },

      //招聘
      {
        path:'/contactManage',
        component:ContactManage
      },
      //增加招聘
      {
        path:'/contact/add',
        component:ContactAdd
      },
      //修改招聘
      {
        path:'/contact/edit/:id',
        component:ContactEdit
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

//挂载路由导航守卫
router.beforeEach((to,from,next) =>{
  if(to.path === '/login') return next();
  //获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if(!tokenStr) return next('/login');
  next();
})

export default router
